/*
   Copyright (c) 2020, zoomdy
   MicroAmpereII is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2.
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
   EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
   MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
   See the Mulan PSL v2 for more details.
 */

#include "pt/pt.h"
#include "pt/timer.h"
#include "pt/delay.h"

#include "hal_tick.h"
#include "hal_led.h"
#include "hal_senser.h"

typedef struct
{
    struct pt pt;
    struct timer timer;
    hal_led_t led;
}app_led_t;

static app_led_t app_led;


void app_led_init(void)
{
    app_led_t* self = &app_led;

    PT_INIT(&self->pt);
}


PT_THREAD(app_led_task(void))
{
    app_led_t* self = &app_led;

    PT_BEGIN(&self->pt);

    while(1) {
        if(hal_senser_get_chnl() != HAL_SENSER_CHNL_NULL) {
            self->led = HAL_LED_R;
        } else {
            self->led = HAL_LED_B;
        }

        hal_led_on(self->led);
        PT_DELAY(&self->pt, &self->timer, 100);
        hal_led_off(self->led);
        PT_DELAY(&self->pt, &self->timer, 100);
    }

    PT_END(&self->pt);
}
